//-----------------------------------------------
//测试TCP server
#include "tcp_server.h"
#include "iomanger.h"

namespace tt01{

    void test_server(){
        learnco::IOmanger iom;
        iom.scheduler([](){
            learnco::server::TCPServer::ptr tcp_server (new learnco::server::TCPServer);
            tcp_server->bind("0.0.0.0",8000);
            tcp_server->start();
        });
    }

}//namespace 
//-----------------------------------------------


//-----------------------------------------------
//echo sever
#include "tcp_server.h"
#include "iomanger.h"
#include "log.h"

namespace ee01{
learnco::Logger::ptr g_logger = learnco::LoggerMgr::GetInstance()->getLogger("user");

class EchoServer : public learnco::server::TCPServer{

public:

protected:
    void handleClient(int client_sock) override;
};

void EchoServer::handleClient(int client_sock){
    std::string buf;
    buf.resize(40960);

    int rt = recv(client_sock, &buf[0], buf.size(), 0);
    
    
    LEARNCO_LOG_DEBUG(g_logger) << "recv rt=" << rt << " errno=" << errno;

    if(rt <= 0) {
        return;
    }

    buf.resize(rt);
    LEARNCO_LOG_DEBUG(g_logger) << buf;


    rt = send(client_sock, &buf[0], rt, 0);
    LEARNCO_LOG_DEBUG(g_logger) << "send rt=" << rt << " errno=" << errno;

    if(rt <= 0) {
        return;
    }

    close(client_sock);
}

void test_echoserver(){
    learnco::IOmanger iom;
    iom.scheduler([](){
        learnco::server::TCPServer::ptr tcp_server (new EchoServer);
        tcp_server->bind("0.0.0.0",8000);
        tcp_server->start();
    });
}

}//namespace 
//-----------------------------------------------

int main()
{
    //tt01::test_server();

    ee01::test_echoserver();

    return 0;
}